import {_decorator, Component, Sprite, UITransform} from 'cc';
import {
    CONTROLLER_ENUM,
    DIRECTION_ENUM,
    DIRECTION_ORDER_ENUM,
    ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM,
    EVENT_ENUM,
    PARAMS_NAME_ENUM
} from "../Enums";
import EventManager from "../Runtime/EventManager";
import {StateMachine} from "./StateMachine";
import {IEntity} from "../Levels";
import {TILE_HEIGHT, TILE_WIDTH} from "../Scripts/Tile/TileManager";

const { ccclass, property } = _decorator;

@ccclass('EntityManager')
export class EntityManager extends Component {
    x:number = 0;
    y:number = 0;

    fsm: StateMachine;
    private _direction: DIRECTION_ENUM;
    private _state: ENTITY_STATE_ENUM;
    private _type: ENTITY_TYPE_ENUM;

    get direction(){ return this._direction; }
    set direction(newDirection){
        console.log(this.direction, '->', newDirection);
        this._direction = newDirection;
        this.fsm.setParam(PARAMS_NAME_ENUM.DIRECTION, DIRECTION_ORDER_ENUM[this.direction]);
    }
    get state(){ return this._state; }
    set state(newState){
        this._state = newState;
        this.fsm.setParam(newState, true);
    }

    async init(param: IEntity) {
        const sprite = this.node.addComponent(Sprite);
        sprite.sizeMode = Sprite.SizeMode.CUSTOM;
        const transform = this.getComponent(UITransform);
        transform.setContentSize(TILE_WIDTH*4, TILE_HEIGHT*4);

        this.x = param.x;
        this.y = param.y;
        this.type = param.type;
        this.direction = param.direction;
        this.state = param.state;
    }

    update() {
        this.node.setPosition(this.x * TILE_WIDTH - 1.5*TILE_WIDTH, -this.y * TILE_HEIGHT + 1.5*TILE_HEIGHT);
    }
}

